package com.iiapk.gridnew;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Handler.Callback;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.preference.Preference.OnPreferenceChangeListener;

import com.iiapk.gridnew.view.SimpleAlertDialog;

public class SettingPreferences extends PreferenceActivity implements OnPreferenceChangeListener{
	
	private LoadClearCacheTask loadClearCacheTask;
	
	protected ProgressDialog mPdLoading;
	public static final int DIALOG_BEGIN = 0;
	public static final int DIALOG_END = 1;
	public static final int STATUS_FINISHED = 3;
	protected Handler mHandler = new Handler(new Callback() {
		@Override
		public boolean handleMessage(Message msg) {
			switch (msg.what) {
			case DIALOG_BEGIN:
				mPdLoading.show();
				return true;
			case DIALOG_END:
				mPdLoading.dismiss();
				return true;
			}
			return false;
		}
	});
	
	private void setupLoadingDialog() {
		mPdLoading = new ProgressDialog(this);
		mPdLoading.setTitle(R.string.dialog_metion);
		mPdLoading.setMessage(getString(R.string.dialog_metion_text));
		mPdLoading.setCancelable(false);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);
		Preference pre_checkupdate = findPreference("pre_checkupdate");
		pre_checkupdate.setSummary(getString(R.string.pre_checkupdate_summary)+"1.0");
		setupLoadingDialog();
	}

	@Override
	public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,Preference preference) {
		if(preference.getKey()!=null){
			if(preference.getKey().equals("pre_about")){
				SimpleAlertDialog.getInstance(this).initSimple(R.string.dialog_about_text);
			}else if(preference.getKey().equals("pre_mindme")){
				beginLoadClearCacheTask();
			}else if(preference.getKey().equals("pre_checkupdate")){
				beginLoadClearCacheTask();
			}else if(preference.getKey().equals("pre_clearcache")){
				beginLoadClearCacheTask();
			}
		}
		return super.onPreferenceTreeClick(preferenceScreen, preference);
	}

	@Override
	public boolean onPreferenceChange(Preference preference, Object newValue) {
		/*if(preference.getKey().equals("")){  
	    } */ 
		return false;
	}
	
	@Override
    protected void onPause() {
    	super.onPause();
    	overridePendingTransition(R.anim.hold,R.anim.push_bottom_out);
    }
	
	private void beginLoadClearCacheTask() {
		if (loadClearCacheTask != null) {
			loadClearCacheTask.cancel(true);
		}
		loadClearCacheTask = new LoadClearCacheTask();
		loadClearCacheTask.execute();
	}
	
	class LoadClearCacheTask extends AsyncTask<Integer, Void, Void> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			mHandler.sendEmptyMessage(DIALOG_BEGIN);
		}
		@Override
		protected Void doInBackground(Integer... params) {
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return null;
		}
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			mHandler.sendEmptyMessage(DIALOG_END);
		}
	}

}
